package com.i5tong.dbviewerlibrary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Steven on 2015/6/29 0029.
 */
public class DbFileRecyclerViewAdapter extends RecyclerView.Adapter<DbFileRecyclerViewAdapter.ViewHolder> {

    public List<String> mStrings;
    public OnDbFileChoose onDbFileChoose;

    public DbFileRecyclerViewAdapter(Context context, List<String> mStrings) {
        this.onDbFileChoose = (OnDbFileChoose) context;
        this.mStrings = mStrings;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.mTextView.setText(mStrings.get(position));
    }

    @Override
    public int getItemCount() {
        return mStrings.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mTextView = (TextView) view.findViewById(android.R.id.text1);
            mTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onDbFileChoose.click(((TextView) v).getText().toString());
                }
            });
        }
    }

    public interface OnDbFileChoose {
        void click(String filename);
    }
}
